package com.rabbitmq.ttl;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.util.ConnectionUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import static com.rabbitmq.common.Constant.NUM10;
import static com.rabbitmq.common.Constant.NUM2;

/**
 * 消息过期（消息自身设置）
 */
@Slf4j
public class MsgTTL1b {
    /**
     * 交换器名称
     */
    private static final String EXCHANGE_NAME = "exchange_demo";
    /**
     * 路由键
     */
    private static final String ROUTING_KEY = "routingkey_demo";
    /**
     * 队列名称
     */
    private static final String QUEUE_NAME = "msgTTL1b_demo";

    public static void main( String[] args ) {
        sendTTLMessage();
        try {
            // 设置为5秒，不会超时，可以接收到消息
//            TimeUnit.SECONDS.sleep(NUM5);
            // 设置为10秒，TTL会超时，消息从队列中删除了，不会接收到消息
            TimeUnit.SECONDS.sleep(NUM10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        consumeTTLMessage();
    }

    private static void sendTTLMessage() {
        try {
            Connection connection = ConnectionUtils.getConnection();
            Channel channel = connection.createChannel();
            // 创建一个 type="direct" 、持久化的、非自动删除的交换器
            channel.exchangeDeclare(EXCHANGE_NAME, "direct", true, false, null);
            // 创建一个持久化、非排他的、非自动删除的队列
            channel.queueDeclare(QUEUE_NAME, true, false, false, null);
            // 将交换器与队列通过路由键绑定
            channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY);
            // 发送一条持久化的消息: hello world !
            String message = "Hello World !";

            AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties.Builder();
            builder.deliveryMode(NUM2);
            builder.expiration("6000");
            AMQP.BasicProperties properties = builder.build();
            // properties中包括了expiration属性
            channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY, false,
                    properties, message.getBytes());

            ConnectionUtils.close(channel, connection);
        } catch (IOException e) {
            log.info("catch IOException");
        } catch (TimeoutException e) {
            log.info("catch TimeoutException");
        }
    }

    public static void consumeTTLMessage() {
        try {
            Connection connection = ConnectionUtils.getConnection();
            Channel channel = connection.createChannel();

            QueueingConsumer consumer = new QueueingConsumer(channel);
            channel.basicConsume(QUEUE_NAME, true, consumer);
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String message = new String(delivery.getBody());
            System.out.println(" [X] Received '" + message + "'");

            channel.close();
            connection.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
